﻿namespace Deppton.Mobile.Views
{
    using System;
    using System.Windows.Forms;

    public partial class QuestionForm : DepptonForm
    {
        private bool controlsEnabled = true;

        public QuestionForm()
        {
            this.InitializeComponent();
        }

        public virtual bool ControlsEnabled
        {
            get
            {
                return this.controlsEnabled;
            }
            set
            {
                this.controlsEnabled = value;
            }
        }

        public Func<string, bool> ConditionEvaluator
        {
            get;
            set;
        }

        public string Description
        {
            get
            {
                return this.DescriptionLabel.Text;
            }

            set
            {
                this.DescriptionLabel.Text = value;
            }
        }

        public bool AllowGoPrevious
        {
            get
            {
                return this.PreviousActionMenuItem.Enabled;
            }

            set
            {
                this.PreviousActionMenuItem.Enabled = value;
            }
        }

        public virtual string Value
        {
            get;
            set;
        }

        public bool AllowGoNext
        {
            get
            {
                return this.NextActionMenuItem.Enabled;
            }

            set
            {
                this.NextActionMenuItem.Enabled = value;
            }
        }

        public bool AllowFinish
        {
            get
            {
                return this.FinishMenuItem.Enabled;
            }

            set
            {
                this.FinishMenuItem.Enabled = value;
            }
        }

        public QuestionFormAction RequestedAction
        {
            get;
            private set;
        }

        private void PreviousActionMenuItemClick(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.RequestedAction = QuestionFormAction.GoPrevious;
            this.Close();
        }

        private void NextActionMenuItemClick(object sender, System.EventArgs e)
        {
            if (!this.ConditionEvaluator.Invoke(this.Value))
            {
                this.ShowConditionNotMet();
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.RequestedAction = QuestionFormAction.GoNext;
            this.Close();
        }

        private void ShowConditionNotMet()
        {
            MessageBox.Show("La respuesta no cumple con los requerimientos.");
        }

        private void BackMenuItemClick(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.RequestedAction = QuestionFormAction.GoBack;
            this.Close();
        }

        private void FinishMenuItemClick(object sender, System.EventArgs e)
        {
            if (!this.ConditionEvaluator.Invoke(this.Value))
            {
                this.ShowConditionNotMet();
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.RequestedAction = QuestionFormAction.Finish;
            this.Close();
        }
    }
}